import {server} from './index'

const Http = (method: string, url: string, params: any) => {
    let paramsLink;
    if(params){
        paramsLink = Object.keys(params).map(key => {
            return `${key}=${params[key]}`
        }).join('&');
    }


    let ip:string = url + (method=="POST"?"":'?'+paramsLink);
    let option:any = {
        method: method,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        body: method=="POST"?paramsLink:{}
    };

    return new Promise((resolve, reject) => {
        fetch(ip, option).then(response => {
            return response.json();
        }).then(data => {
            resolve(data);
        }).catch(err => {
            reject(err);
        })
    })
}

export default Http;


